package com.example.yls.demoa;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btnStart;
    private Button btnStop;
    private Button btnBinder;
    private Button btnUnBinder;
    private Button btnAdd;
    private Button btnTime;
    private MyBinder myBinder;
    private ServiceConnection serviceConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart=findViewById(R.id.btn_start);
        btnStop=findViewById(R.id.btn_stop);
        btnBinder=findViewById(R.id.btn_binder);
        btnUnBinder=findViewById(R.id.btn_unbinder);
        btnAdd=findViewById(R.id.btn_add);
        btnTime=findViewById(R.id.btn_time);
        myBinder=new MyBinder();
        serviceConnection =new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.e("MainActivity","onServiceConnected");
                myBinder=(MyBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.e("MainActivity","onServiceDisconnected");
            }
        };

        btnBinder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);
            }
        });


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                startService(intent);
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                stopService(intent);
            }
        });

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int result=myBinder.add(10,20);
                Toast.makeText(MainActivity.this,"结果"+result,Toast.LENGTH_LONG).show();
            }
        });

        btnTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String time=myBinder.getTime();
                Toast.makeText(MainActivity.this,"当前时间"+time,Toast.LENGTH_LONG).show();
            }
        });
    }
}
